import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:ncov_2019/api/news_model.dart';
import 'package:ncov_2019/beans/Entity.dart';
import 'package:ncov_2019/commom/commom.dart';
import 'package:ncov_2019/http/dio_manager.dart';
import 'package:ncov_2019/pages/settings/TestDemo.dart';
import 'package:ncov_2019/pages/settings/machine_settings.dart';
import 'package:ncov_2019/widget/view/LoadingDialog.dart';

class NewsCard extends StatelessWidget {
  final App_esp_ip model;
  final EdgeInsetsGeometry padding;
  final bool isNew;

  NewsCard(
    this.model, {
    @required this.padding,
    this.isNew = false,
  });

  @override
  Widget build(BuildContext context) {
//    double createTimeDouble =
//        strNoEmpty('${model?.createTime}') ? model.createTime / 1000 : 0;
//    int createTime = int.parse('${stringDisposeWithDouble(createTimeDouble)}');
//
//    bool isTimeStr =
//        DateTime.now().millisecondsSinceEpoch - model?.createTime < 43200000;
    return Container(
      padding: padding,
      alignment: Alignment.centerLeft,
      width: winWidth(context),
      child: new FlatButton(
        onPressed: () {
          // showModalBottomSheet(context);
          Navigator.of(context)
              .push(new MaterialPageRoute(builder: (context) {
            return new TestDemo(model: model,);
          }));

        },
        /*长按*/
        onLongPress: () {
          print("长按文本");
          showDialog(context: context,
              builder: (BuildContext context) {
                return CupertinoAlertDialog(
//                  title: Text("这是一个iOS风格的对话框"),
                  content: SizedBox(
                    child:  new ButtonBarLessDefault()
                  ),
                  actions: <Widget>[
                    CupertinoDialogAction(
                      child: Text("取消"),
                      onPressed: () {
                        Navigator.pop(context);
                        print("取消");
                      },
                    ),
//                    CupertinoDialogAction(
//                      child: Text("确定"),
//                      onPressed: () {
//                        print("确定");
//                      },
//                    ),
                  ],
                );
              }
          );
        },
        color: Colors.white,
        padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(5.0)),
        ),
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Wrap(
              direction: Axis.horizontal,//不设置默认为horizontal
              alignment: WrapAlignment.center,//沿主轴方向居中
              spacing: 0.0,//主轴（水平）方向间距
              runSpacing: 4.0,//纵轴（垂直）方向间距
              children: <Widget>[
                new Visibility(
                  visible: model.line,
                  child: new Container(
                    decoration: BoxDecoration(
                        color: Colors.green,
                        borderRadius: BorderRadius.all(Radius.circular(5.0))),
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(horizontal: 3.0, vertical: 2),
                    margin: EdgeInsets.only(right: 5.0),
                    child: new Text(
                      '在线',
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ),
                new SizedBox(
                  width:
                      isNew ? winWidth(context) - 120 : winWidth(context) - 70,
                  child: new Text(
                    '${model?.productName ?? '无名'}',
                    style:
                        TextStyle(fontSize: 16.0, fontWeight: FontWeight.w600),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                )
        ],
            ),
//            new Padding(
//              padding: EdgeInsets.symmetric(vertical: 10.0),
//              child: new Text(
//                '${model?.summary ?? '未知'}',
//                maxLines: 5,
//                overflow: TextOverflow.ellipsis,
//                style: TextStyle(color: Color(0xff999999), fontSize: 12.0),
//              ),
//            ),
//            new Row(
//              mainAxisAlignment: MainAxisAlignment.spaceBetween,
//              children: <Widget>[
//                new Text(
//                  '时间：${isTimeStr ? model?.pubDateStr ?? '未知' : formatTimeStampToString(createTime) ?? '未知'}',
//                  style: TextStyle(color: Color(0xff999999), fontSize: 13.0),
//                ),
//                new Text(
//                  '来源：${model?.infoSource ?? '未知'}',
//                  style: TextStyle(color: Color(0xff999999), fontSize: 13.0),
//                ),
//              ],
//            )
          ],
        ),
      ),
    );
  }
}
showModalBottomSheet(BuildContext context) {
  showBottomSheet(
    context: context,
    builder: (context) => ListView(
      // 生成一个列表选择器
        children: List.generate(
          5,
              (index) => InkWell(
              child: Container(alignment: Alignment.center, height: 60.0, child: Text('Item ${index + 1}')),
              onTap: () {
                print('tapped item ${index + 1}');
                Navigator.pop(context);
              }),
        )),
  );
}

/*
* ButtonBar 默认的实例,无状态
* */
class ButtonBarLessDefault extends StatelessWidget {
  final widget;
  final parent;
  final App_esp_ip model;

  const ButtonBarLessDefault([this.widget, this.parent,this.model])
      : super();

  @override
  Widget build(BuildContext context) {
    return Container(
        margin:  EdgeInsets.symmetric(vertical: 0.0),
        height: 200.0,
        child:  Scrollbar(child:ListView(
            scrollDirection: Axis.vertical, // 水平listView
            children: <Widget>[
              RaisedButton(child: Text('解绑此设备'),color: Colors.red,
                onPressed: () {
                  Navigator.pop(context);
                  print("解绑此设备");
                  showDialog(context: context,
                      builder: (BuildContext context) {
                        return CupertinoAlertDialog(
                          title: Text("不可逆操作提示"),
                          content: SizedBox(
                              child:  Text("确定要解绑此台设备吗"),
                          ),
                          actions: <Widget>[
                            CupertinoDialogAction(
                              child: Text("取消"),
                              onPressed: () {
                                Navigator.pop(context);
                                print("取消");
                              },
                            ),
                            CupertinoDialogAction(
                              child: Text("确定"),

                              onPressed: () {
                                print("确定");
                                showDialog(
                                    context: context,
                                    barrierDismissible: false,
                                    builder: (BuildContext context) {
                                      return new LoadingDialog(
                                        text: "机器解绑中…",
                                      );
                                    });

                                deleteDevice(() {

                                },model.id);


                              },
                            ),
                          ],
                        );
                      }
                  );
                },

              ),
              RaisedButton(child: Text('重命名此设备'),color: Colors.red,

                onPressed: () {
                  Navigator.pop(context);
                  print("重命名此设备");
                  showDialog(context: context,
                      builder: (BuildContext context) {
                        return CupertinoAlertDialog(
                          title: Text("不可逆操作提示"),
                          content: Card(
                          elevation: 0.0,
                          child: Column(
                            children: <Widget>[
                              TextField(
                                decoration: InputDecoration(
                                hintText: '请输入用户名',
                                filled: true,
                                ),
                                ),
                                ],
                            ),
                          ),

                          actions: <Widget>[
                            CupertinoDialogAction(
                              child: Text("取消"),
                              onPressed: () {
                                Navigator.pop(context);
                                print("取消");
                              },
                            ),
                            CupertinoDialogAction(
                              child: Text("确定"),
                              onPressed: () {
                                print("确定");
                              },
                            ),
                          ],
                        );
                      }
                  );
                },

              ),
              RaisedButton(child: Text('分享此设备'),color: Colors.red,onPressed: ()=>{
              DatePicker.showDateTimePicker(context,
                  showTitleActions: true,
                  minTime:DateTime(2008, 12, 31, 23, 12, 34),
                    maxTime: DateTime(2019, 6, 7,1,1,1), onChanged: (date) {
                       print('change $date');
                }, onConfirm: (date) {
                  print('confirm $date');
                 }, currentTime: DateTime.now(), locale: LocaleType.zh),
              },),

              RaisedButton(child: Text('进行本地连接'),color: Colors.red,onPressed: ()=>{},),

            ]
        )
        ));
  }
}

void deleteDevice(Function callback, String id) async {
  DioManager.singleton.dio
      .get(API.deleteDevice+id)
      .then((response) {
    print(response.toString());
    callback(response);
  });
}